#include"conio.h"
#include"stdio.h"
#include"stdlib.h"
int chen(int k, int n, int x[]);
int main()
{
   clrscr();
   int n,a,i,x[100],k;
   printf("Nhap so phan tu cua mang n=");
   scanf("%d",&n);
   printf("Nhap mang %d so nguyen theo thu tu tang dan\n",n);
   for(i=1;i<=n;i++)
   {
      printf("x[%d]=",i);
      scanf("%d",&a);
      x[i]=a;
   }
   printf("\nIn mang truoc khi chen:");
   for(i=1;i<=n;i++)
   printf("%d\t",x[i]);
   printf("\nNhap so nguyen can chen k=");
   scanf("%d",&k);
   chen(k,n,x);
   printf("\nIn mang sau khi chen:");
   for(i=1;i<=n+1;i++)
   printf("%d\t",x[i]);
   getch();
   return 0;
}
int chen(int k, int n, int x[])
{
   int i,j;
   if(k>x[n]) { x[n+1]=k; return 0; }
   if(k<x[1])
   {
     for(j=n+1;j>=2;j--)
	x[j]=x[j-1];
     x[1]=k;
     return 0;
   }
   for(i=1;i<=n-1;i++)
   {
      if(x[i]<=k && k<=x[i+1])
      {
	 for(j=n+1;j>=i+2;j--)
	    x[j]=x[j-1];
	 x[i+1]=k;
	 return 0;
      }
   }
}
